﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cua_Hang.DTO;
using System.Text.RegularExpressions;
using System.Data;

namespace Cua_Hang.Admin
{
    public partial class QuanLy_DiaChi_CuaHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Load_Ten_CuaHang_();
            }
            List_All_DiaChi_CuaHang();
            this.BT_CapNhat.Visible = false;

        }
        //==ham load all cua hang vao dropdowlist
        void Load_Ten_CuaHang_()
        {
            DDL_TenCuaHang.DataSource = Cua_Hang.BUS.Cua_Hang_BUS.ListCuaHangAll_BUS();
            DDL_TenCuaHang.DataTextField = "Ten_cuahang";
            DDL_TenCuaHang.DataValueField = "ID_cuahang";
            DDL_TenCuaHang.DataBind();

        }
        //---------------------------------------
        //---------------------------------------
        //==load all toa do cua hang vao gridview
        void List_All_DiaChi_CuaHang()
        {
            GV_DiaChiCuaHang.DataSource = Cua_Hang.BUS.DiaChi_CuaHang_BUS.List_DiaChi_TatCa_CuaHang_BUS();
            GV_DiaChiCuaHang.DataBind();
        }
        //ham xoa
        protected void GV_DiaChiCuaHang_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Int32 index = Int32.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "Xoa")
            {
                //String Code = GV_ChiTietCuaHang.DataKeys[index].Value.ToString();
                Cua_Hang.DTO.DiaChi_CuaHang_DTO chang = new DTO.DiaChi_CuaHang_DTO();
                chang.ID_ThongTin_CuaHang = Convert.ToInt32(e.CommandArgument);
                Cua_Hang.BUS.DiaChi_CuaHang_BUS.Xoa_DiaChi_CuaHang_BUS(chang);
                this.List_All_DiaChi_CuaHang();
            }
        }
        //----------them dia chi cua hang------------------------------
        void Them_DiaChiCuaHang_GUI()
        {
            DiaChi_CuaHang_DTO ct = new DiaChi_CuaHang_DTO();
            ct.ID_cuahang = Convert.ToInt32(DDL_TenCuaHang.SelectedValue);
            ct.Duong_CuaHang = TB_TenDuong.Text;
            ct.DienThoai_Cuahang =Convert.ToInt32( TB_DienThoai.Text);
            ct.Email_CuaHang = TB_Email.Text;
            ct.Ten_CuaHang = TB_TenCuaHang.Text;
            ct.Phuong_CuaHang = TB_Phuong.Text;
            ct.Quan_CuaHang = TB_Quan.Text;
            ct.Tp_CuaHang = TB_ThanhPho.Text;
            Cua_Hang.BUS.DiaChi_CuaHang_BUS.Them_DiaChi_CuaHang_BUS(ct);
        }
        //ham them
        protected void BT_Them_Click(object sender, EventArgs e)
        {
            if (KiemTra() == true)
            {
                Cua_Hang.DTO.DiaChi_CuaHang_DTO chang = new DTO.DiaChi_CuaHang_DTO();
                chang.Email_CuaHang = TB_Email.Text;
                chang.DienThoai_Cuahang = Convert.ToInt32(TB_DienThoai.Text.ToString());
                chang.Ten_CuaHang = TB_TenCuaHang.Text;
            
            
                if (Cua_Hang.BUS.DiaChi_CuaHang_BUS.KiemTraTrung_DiaChi_CuaHang_BUS(chang) == false)
                {
                    this.Them_DiaChiCuaHang_GUI();
                    this.List_All_DiaChi_CuaHang();
                    this.ClearTextBox();
                }
                else
                {
                    Response.Write(@"<script language = 'javascript'>alert ('Tên Cửa Hàng or Email or số điện thoại tồn tại !'); </script>");
                }
            }

            //if(KiemTra()==true){
            //this.Them_DiaChiCuaHang_GUI();
            //this.List_All_DiaChi_CuaHang();
            //}
        }
        //ham cap nhat
        //============= chuyen List thanh DataTable===========================
        //
        public DataTable ConvertList_DiaChi_DTOToDataTable(List<DiaChi_CuaHang_DTO> lst)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("ID_ThongTin_CuaHang");
            dt.Columns.Add("ID_cuahang");
            dt.Columns.Add("Duong_CuaHang");
            dt.Columns.Add("DienThoai_Cuahang");
            dt.Columns.Add("Email_CuaHang");
            dt.Columns.Add("Ten_CuaHang");
            dt.Columns.Add("Phuong_CuaHang");
            dt.Columns.Add("Quan_CuaHang");
            dt.Columns.Add("Tp_CuaHang");

            for (int i = 0; i < lst.Count; i++)
            {
                dt.Rows.Add(
                        lst[i].ID_ThongTin_CuaHang,
                        lst[i].ID_cuahang,
                        lst[i].Duong_CuaHang,
                        lst[i].DienThoai_Cuahang,
                        lst[i].Email_CuaHang,
                        lst[i].Ten_CuaHang,
                        lst[i].Phuong_CuaHang,
                        lst[i].Quan_CuaHang,
                        lst[i].Tp_CuaHang

                    );
            }
            return dt;
        }

        //============= ket thuc chuyen List thanh DataTable=======================
        //==============ham hien vao textbox toa do theo id =================
        public DiaChi_CuaHang_DTO ShowChiTiet_DiaChi_CuaHangTheoID(int ID_ThongTin_CuaHang)
        {
            DiaChi_CuaHang_DTO dm = new DiaChi_CuaHang_DTO();
            List<DiaChi_CuaHang_DTO> lst = BUS.DiaChi_CuaHang_BUS.List_DiaChi_TheoID_BUS(ID_ThongTin_CuaHang);
            DataTable dt = new DataTable();
            dt = ConvertList_DiaChi_DTOToDataTable(lst);
            foreach (DataRow dr in dt.Rows)
            {
                //dm.ID_cuahang = (int)dr["ID_cuahang"];
                //dm.ID_cuahang = (int)dr["ID_cuahang"];
                dm.Duong_CuaHang = dr["Duong_CuaHang"].ToString();
                dm.DienThoai_Cuahang =Convert.ToInt64(dr["DienThoai_Cuahang"].ToString());
                dm.Email_CuaHang = dr["Email_CuaHang"].ToString();
                dm.Ten_CuaHang = dr["Ten_CuaHang"].ToString();
                dm.Phuong_CuaHang = dr["Phuong_CuaHang"].ToString();
                dm.Quan_CuaHang = dr["Quan_CuaHang"].ToString();
                dm.Tp_CuaHang = dr["Tp_CuaHang"].ToString();

            }
            return dm;
        }
        //---
        //----ham cap nhat dia chi cua hang
        private void CapNhatDiaChiCuaHang()
        {
            if(KiemTra()){
            DiaChi_CuaHang_DTO dm = new DiaChi_CuaHang_DTO();
            //LayThongTinChiTietCuaHang(ref dm);
            dm.ID_ThongTin_CuaHang = Convert.ToInt32(TB_DiaChi_CuaHang.Text);
            dm.Duong_CuaHang = TB_TenDuong.Text;
            dm.DienThoai_Cuahang = Convert.ToInt64(TB_DienThoai.Text);
            dm.Email_CuaHang = TB_Email.Text;
            dm.Ten_CuaHang = TB_TenCuaHang.Text;
            dm.Phuong_CuaHang = TB_Phuong.Text;
            dm.Quan_CuaHang = TB_Quan.Text;
            dm.Tp_CuaHang = TB_ThanhPho.Text;
            Cua_Hang.BUS.DiaChi_CuaHang_BUS.Sua_DiaChi_CuaHang_BUS(dm);
            }

        }
        //-------------
        void ClearTextBox()
        {
            TB_DiaChi_CuaHang.Text = "";
            TB_DienThoai.Text = "";
            TB_Email.Text = "";
            TB_Phuong.Text = "";
            TB_Quan.Text = "";
            TB_TenCuaHang.Text = "";
            TB_TenDuong.Text = "";
            TB_ThanhPho.Text = "";
        }
        //--------------
        protected void BT_CapNhat_Click(object sender, EventArgs e)
        {
            if(KiemTra())
            {
                this.CapNhatDiaChiCuaHang();
                this.List_All_DiaChi_CuaHang();
                this.BT_Them.Visible = true;
                  this.ClearTextBox();

            }
        }
        //---ham kiem tra so nguyen-----------------------
        private bool KiemTrasonguyen(string pValue)
        {
            //ham kiem tra so nguyen
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
            
            
        }
        //---ham kiem tra email---
        private bool KiemTraEmail(string email)
        {
            try
            {
                int nFirt = int.Parse(email.Substring(0, 1));
                return false;
            }
            catch
            { 
                string pattern = @"^[_a-zA-Z0-9][_.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[_.a-zA-Z0-9]+)*\.
                 (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|vn|[a-zA-Z]{2})$";
                //Regular expression object
                Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
                //boolean variable to return to calling method
                bool valid = false;

                //make sure an email address was provided
                if (string.IsNullOrEmpty(email))
                {
                    valid = false;
                }
                else
                {
                    //use IsMatch to validate the address
                    valid = check.IsMatch(email.ToLower());
                }
                return valid;
            }
            
        }
        //====================ham kiem tra================================
        private bool KiemTra()
        {
            if (this.TB_DienThoai.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập số điện thoại:!'); </script>");

                return false;
            }
            //if (Convert.ToInt32(this.TB_DienThoai.Text.ToString())<= 13)
            //{
            //    Response.Write(@"<script language = 'javascript'>alert (' số điện thoại phải lớn hơn 1:!'); </script>");

            //    return false;
            //}
            if (this.KiemTrasonguyen(TB_DienThoai.Text)==false)
            {
                Response.Write(@"<script language = 'javascript'>alert ('số điện thoại phải là số nguyên:!'); </script>");

                return false;
            }
            if (this.TB_Email.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập Email!'); </script>");

                return false;
            }
            if (KiemTraEmail(this.TB_Email.Text)==false)
            {
                Response.Write(@"<script language = 'javascript'>alert (' Email chưa hợp lệ!'); </script>");

                return false;
            }
            if (this.TB_Phuong.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập Phường!'); </script>");

                return false;
            }
            if (this.TB_Quan.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập Quận!'); </script>");

                return false;
            }
            if (this.TB_TenCuaHang.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập Tên cửa hàng!'); </script>");

                return false;
            }
            if (this.TB_TenDuong.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập Tên Đường!'); </script>");

                return false;
            }
            if (this.TB_ThanhPho.Text.Trim() == "")
            {
                Response.Write(@"<script language = 'javascript'>alert ('Bạn chưa nhập Tên thành Phô`!'); </script>");

                return false;
            }
            return true;

        }

        protected void TB_TenCuaHang_TextChanged(object sender, EventArgs e)
        {

        }
        //-------------
        protected void GV_DiaChiCuaHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ID_ThongTin_CuaHang = Convert.ToInt32(GV_DiaChiCuaHang.SelectedRow.Cells[0].Text);
            TB_DiaChi_CuaHang.Text = ID_ThongTin_CuaHang.ToString();
            DiaChi_CuaHang_DTO dmn = new DiaChi_CuaHang_DTO();
            dmn = ShowChiTiet_DiaChi_CuaHangTheoID(ID_ThongTin_CuaHang);
            TB_TenCuaHang.Text = dmn.Ten_CuaHang.ToString();
            TB_TenDuong.Text = dmn.Duong_CuaHang.ToString();
            TB_DienThoai.Text = dmn.DienThoai_Cuahang.ToString();
            TB_Email.Text = dmn.Email_CuaHang.ToString();
            //TB_DiaChi_CuaHang.Text = dmn.Duong_CuaHang.ToString();
            TB_Phuong.Text = dmn.Phuong_CuaHang.ToString();
            TB_Quan.Text = dmn.Quan_CuaHang.ToString();
            TB_ThanhPho.Text = dmn.Tp_CuaHang.ToString();
            this.BT_Them.Visible = false;
            this.BT_CapNhat.Visible = true;
        }

        protected void GV_DiaChiCuaHang_PageIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GV_DiaChiCuaHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV_DiaChiCuaHang.PageIndex = e.NewPageIndex;
            this.List_All_DiaChi_CuaHang();
        }
        //----



    }
}